package com.ipan.weixin.msg.general;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 图文消息
 * 
 * @author iPan
 * @version 2013-12-24
 */
public class NewsMsg extends PlainMsg {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -3803955025446344577L;
	
	/** ArticleCount元素 */
	private int articleCount;
	
	/** Articles元素 */
	private Articles ariticles = null;
	
	public NewsMsg() {
		super(TransportType.REPLY);
	}
	
	public NewsMsg(Document doc) {
		this();
		fromXml(doc);
	}
	
	public int getArticleCount() {
		return articleCount;
	}

	public void setArticleCount(int articleCount) {
		this.articleCount = articleCount;
	}

	public Articles getAriticles() {
		return ariticles;
	}

	public void setAriticles(Articles ariticles) {
		this.ariticles = ariticles;
	}

	// Articles元素
	public static class Articles {
		
		/** item元素列表 */
		private List<Item> items;

		public List<Item> getItems() {
			return items;
		}

		public void setItems(List<Item> items) {
			this.items = items;
		}
		
		public void addItem(Item item) {
			checkItem();
			items.add(item);
		}
		
		public void addItem(String title, String description, String picUrl, String url) {
			checkItem();
			items.add(new Item(title, description, picUrl, url));
		}
		
		public synchronized void checkItem() {
			if (items == null) {
				items = new ArrayList<Item>();
			}
		}
		
		public int size() {
			return (items != null) ? items.size() : 0;
		}
		
	}
	
	// item元素
	public static class Item {
		/** 图文消息标题 */
		private String title;
		
		/** 图文消息描述 */
		private String description;
		
		/** 图片链接，支持JPG、PNG格式，较好的效果为大图360*200，小图200*200  */
		private String picUrl;
		
		/** 点击图文消息跳转链接 */
		private String url;
		
		public Item() {}
		
		public Item(String title, String description, String picUrl, String url) {
			this.title = title;
			this.description = description;
			this.picUrl = picUrl;
			this.url = url;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public String getPicUrl() {
			return picUrl;
		}

		public void setPicUrl(String picUrl) {
			this.picUrl = picUrl;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}
		
	}

	@Override
	public void fromXml(Document doc) {
		if (doc == null) {
			return ;
		}
		
		super.fromXml(doc);
		Element root = doc.getRootElement();
		String articleCountStr = root.elementTextTrim(EL_ARTICLE_COUNT);
		int count = (StringUtils.isNotBlank(articleCountStr)) ? Integer.valueOf(articleCountStr) : 0;
		this.articleCount = count;
		Element elArticles = root.element(EL_ARTICLES);
		if (elArticles == null) {
			return ;
		}
		
		Articles arts = new Articles();
		this.setAriticles(arts);
		List<?> list = elArticles.elements(EL_ITEM);
		if (list != null && list.size() > 0) {
			for (Object o : list) {
				Element item = (Element) o;
				String title = item.elementTextTrim(EL_TITLE);
				String description = item.elementTextTrim(EL_DESCRIPTION);
				String picUrl = item.elementTextTrim(EL_PIC_URL);
				String url = item.elementTextTrim(EL_URL);
				arts.addItem(title, description, picUrl, url);
			}
		}
	}
	
	@Override
	public String toXml(boolean nonEmpty) {
		StringBuilder buf = new StringBuilder("<xml>");
		buf.append(super.toXml(nonEmpty));
		buf.append(wrapElementHead(EL_MSG_TYPE)).append(TYPE_NEWS).append(wrapElementEnd(EL_MSG_TYPE));
		buf.append(wrapElementHead(EL_ARTICLE_COUNT)).append(this.articleCount).append(wrapElementEnd(EL_ARTICLE_COUNT));
		buf.append(wrapElementHead(EL_ARTICLES));
		if (this.ariticles != null && this.ariticles.size() > 0) {
			List<Item> items = this.ariticles.getItems();
			for (Item item : items) {
				buf.append(wrapElementHead(EL_ITEM));
				if (nonEmpty) {
					if (StringUtils.isNotEmpty(item.getTitle())) {
						buf.append(wrapElementHead(EL_TITLE)).append(item.getTitle()).append(wrapElementEnd(EL_TITLE));
					}
					if (StringUtils.isNotEmpty(item.getDescription())) {
						buf.append(wrapElementHead(EL_DESCRIPTION)).append(item.getDescription()).append(wrapElementEnd(EL_DESCRIPTION));
					}
					if (StringUtils.isNotEmpty(item.getPicUrl())) {
						buf.append(wrapElementHead(EL_PIC_URL)).append(item.getPicUrl()).append(wrapElementEnd(EL_PIC_URL));
					}
					if (StringUtils.isNotEmpty(item.getUrl())) {
						buf.append(wrapElementHead(EL_URL)).append(item.getUrl()).append(wrapElementEnd(EL_URL));
					}
				} else {
					buf.append(wrapElementHead(EL_TITLE)).append(getStringValue(item.getTitle())).append(wrapElementEnd(EL_TITLE));
					buf.append(wrapElementHead(EL_DESCRIPTION)).append(getStringValue(item.getDescription())).append(wrapElementEnd(EL_DESCRIPTION));
					buf.append(wrapElementHead(EL_PIC_URL)).append(getStringValue(item.getPicUrl())).append(wrapElementEnd(EL_PIC_URL));
					buf.append(wrapElementHead(EL_URL)).append(getStringValue(item.getUrl())).append(wrapElementEnd(EL_URL));
				}
				buf.append(wrapElementEnd(EL_ITEM));
			}
		}
		buf.append(wrapElementEnd(EL_ARTICLES));
		return buf.append("</xml>").toString();
	}
	
}

/*
回复：
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[title1]]></Title> 
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
</xml> 
*/